package com.king.headline.UI;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.king.headline.R;
import com.king.headline.base.BaseActivity;
import com.king.headline.beans.UserInfo;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.util.AccountManager;
import com.king.headline.util.CustomToast;
import com.king.headline.util.DialogUtils;
import com.king.headline.util.KeyBoardUtils;
import com.king.headline.util.UIUtils;
import com.king.headline.util.Utils;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 找回密码
 *
 * @author king
 */
public class BindPhoneActivity extends BaseActivity implements OnClickListener {
    private TextView tv_title;//标题
    private EditText et_userName;//手机号
    private TextView bt_getPassWord;//动态获取密码
    private EditText et_password;//密码
    private Button bt_loging;
    private ProgressDialog pDialog;
    private TimerTask task;
    private int TIME = 0x852147;
    private int time = 60;//60秒
    private long timeQuite = 0;
    private Timer timer;
    private Boolean firstTimeClick = true;//是否是首次点击获取
    private boolean isNoPhoneBind;

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == TIME) {
                time--;
                if (time <= 0) {
                    bt_getPassWord.setClickable(true);
                    bt_getPassWord.setText("获取验证码");
                } else {
                    bt_getPassWord.setText(String.valueOf(time) + "秒后再试");
                }
            }
        }

        ;
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UIUtils.addActivity(this);
        baseSetContentView(R.layout.activity_bindphone);
        initView();
        task = new TimerTask() {

            @Override
            public void run() {
                handler.sendEmptyMessage(TIME);
            }
        };
    }

    private void initView() {
        setBackIcon(true);
        tv_title = (TextView) findViewById(R.id.tv_title);
        et_userName = (EditText) findViewById(R.id.et_userName);
        bt_getPassWord = (TextView) findViewById(R.id.bt_getPassWord);
        et_password = (EditText) findViewById(R.id.et_password);
        bt_loging = (Button) findViewById(R.id.bt_loging);
        bt_getPassWord.setOnClickListener(this);
        bt_loging.setOnClickListener(this);
        UserInfo userinfo = AccountManager.getInstance().getUser();
        if (userinfo!=null && TextUtils.isEmpty(userinfo.telephone)){
            isNoPhoneBind = true;
        }
        if (isNoPhoneBind){
            tv_title.setText("绑定手机");
        }else {
            tv_title.setText("换绑手机");
        }
    }

    private void getCodeTak(String phone) {
        pDialog = ProgressDialog.show(BindPhoneActivity.this, "", "请稍候~");
        NetWork.getInstance().getCode(phone, new Callback() {
            @Override
            public void success(String result) {
                Utils.dismissDialog(pDialog);
                timer = new Timer();
                time = 60;
                if (firstTimeClick) {
                    firstTimeClick = false;
                    timer.schedule(BindPhoneActivity.this.task, 0, 1000);
                }
                bt_getPassWord.setClickable(false);
                et_password.requestFocus();
                et_password.isFocused();
                CustomToast.showToast("验证码已发送到您手机上~");
            }

            @Override
            public void notice(JSONObject jsonObject, String message) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(message);
                bt_getPassWord.setClickable(true);
                bt_getPassWord.setText("获取验证码");
            }

            @Override
            public void failed(String e) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(e);
                bt_getPassWord.setClickable(true);
                bt_getPassWord.setText("获取验证码");
            }
        });
    }

    private void bindTelephone(String encyptdId, String apikey, String telephone, String codeNum) {
        pDialog = ProgressDialog.show(BindPhoneActivity.this, "", "请稍候~");
        NetWork.getInstance().bindTelephone(encyptdId, apikey, telephone, codeNum, new Callback() {
            @Override
            public void success(String result) {
                Utils.dismissDialog(pDialog);
                DialogUtils.showHintDialogOne(BindPhoneActivity.this, result+",请重新登陆", false, new DialogUtils.OnDialogListener() {
                    @Override
                    public void onOk(View v) {
//                        refresh();
                        AccountManager.getInstance().logout();
                        finish();
                    }

                    @Override
                    public void onCancel(View v) {

                    }
                });

            }

            @Override
            public void notice(JSONObject jsonObject, String message) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(message);
            }

            @Override
            public void failed(String e) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(e);
            }
        });


    }

//    public void refresh() {
//        NetWork.getInstance().login(AccountManager.getInstance().getUser().apikey, new Callback() {
//            @Override
//            public void success(String result) {
//                UserInfo userInfo = new Gson().fromJson(result, UserInfo.class);
//                AccountManager.getInstance().setUser(userInfo);
//            }
//
//            @Override
//            public void notice(JSONObject jsonObject, String message) {
//            }
//
//            @Override
//            public void failed(String e) {
//            }
//        });
//    }
    @Override
    public void onClick(View v) {
        String phone = et_userName.getText().toString();
        switch (v.getId()) {
            case R.id.bt_getPassWord:
                if (Utils.isPhoneNumberValid(phone)) {
                    getCodeTak(phone);
                } else {
                    CustomToast.showToast("手机号码输入有误");
                }
                break;
            case R.id.bt_loging:
                String code = et_password.getText().toString().trim();

                //隐藏键盘
                KeyBoardUtils.closeKeybord(et_userName);
                if (!Utils.isPhoneNumberValid(phone)) {
                    DialogUtils.showHintDialogOne(this, "手机号码输入有误");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    DialogUtils.showHintDialogOne(this, "验证码不能为空");
                    return;
                }
                UserInfo userinfo = AccountManager.getInstance().getUser();
                bindTelephone(userinfo.encyptdId, userinfo.apikey, phone, code);
                break;
            case R.id.tv_login_type2:
                Intent intent = new Intent(UIUtils.getContext(), Loging2Activity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
        UIUtils.removeActivity(this);
    }


}
